package com.csun.dsms.controller;

import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.huang.HgInstructionListData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgUser;
import com.csun.dsms.service.HuangGangService;
import com.csun.dsms.utils.pt.HuangGangUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc    用户登录拦截器
 */
@Slf4j
@RestController
@RequestMapping("/hgUser")
public class HgUserController {

    @Resource
    private HuangGangService huangGangService;

    /**
     * i黄冈登录 获取用户的UserId 和身份证号
     * @param oToken
     * @return
     */
    @Authorization
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Wrapper login(@RequestBody String oToken) {
        System.out.println("++++");
        System.out.println(oToken);
        JSONObject aesJsonBean = JSONObject.parseObject(oToken);
        System.out.println(aesJsonBean);
        String iHgToken = aesJsonBean.getString("oToken");
        System.out.println(iHgToken);
        HgUser user = huangGangService.login(iHgToken);
        return WrapMapper.ok(user);
    }



    /**
     * oA平台登录 获取用户的UserId 和身份证号
     * @param ticket
     * @return
     */
    @Authorization
    @RequestMapping(value = "/oaLogin",method = RequestMethod.POST)
    public Wrapper oaLogin(@RequestBody String ticket) {
        log.info("++++");
        System.out.println(ticket);
        JSONObject aesJsonBean = JSONObject.parseObject(ticket);
        System.out.println(aesJsonBean);
        String oaTicket = aesJsonBean.getString("ticket");
        log.info(oaTicket);
        HgUser user = huangGangService.oaLogin(oaTicket);
        return WrapMapper.ok(user);
    }


    @Authorization
    @RequestMapping(value = "/oaLogin2",method = RequestMethod.POST)
    public Wrapper oaLogin2(@RequestBody String ticket) {
        log.info("++++");
        System.out.println(ticket);
        JSONObject aesJsonBean = JSONObject.parseObject(ticket);
        System.out.println(aesJsonBean);
        String oaTicket = aesJsonBean.getString("ticket");
        log.info(oaTicket);
        HgUser user = huangGangService.oaLogin2(oaTicket);
        return WrapMapper.ok(user);
    }

    @Authorization
    @RequestMapping(value = "/syncOaLogin",method = RequestMethod.POST)
    public Wrapper syncOaLogin() {
        int ret = huangGangService.syncOAUser();
        return WrapMapper.ok(ret);
    }

    @Authorization
    @RequestMapping(value = "/syncHgLogin",method = RequestMethod.POST)
    public Wrapper syncHgLogin() {
        int ret = huangGangService.syncHgUser();
        return WrapMapper.ok(ret);
    }

    /*
     * 查询批示
     * */
    @Authorization
    @RequestMapping(value = "/userList",method = RequestMethod.POST)
    public Wrapper userList (@RequestBody HgQueryDto hgQueryDto){
        List<HgUser> query = huangGangService.queryUserList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    @Authorization
    @RequestMapping(value = "/editUser",method = RequestMethod.POST)
    public Wrapper editUser (@RequestBody HgUser hgUser){
        Integer result = huangGangService.update(hgUser);
        List<HgUser> hgUserList = huangGangService.selectAll();
        HuangGangUtil.syncUserList(hgUserList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }

    @Authorization
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public Wrapper addUser (@RequestBody HgUser hgUser){
        Integer result = huangGangService.save(hgUser);
        List<HgUser> hgUserList = huangGangService.selectAll();
        HuangGangUtil.syncUserList(hgUserList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }


    @Authorization
    @PostMapping(value = "/deleteUserById/{id}")
    public Wrapper deleteById (@PathVariable Long id){
        Integer result = huangGangService.deleteByKey(id);
        List<HgUser> hgUserList = huangGangService.selectAll();
        HuangGangUtil.syncUserList(hgUserList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }


}
